package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.io.File;
import java.util.List;


public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
    private Button load_data;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        load_data = (Button) findViewById(R.id.load_data);
        load_data.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续
            Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程。
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor actualimagecursor = managedQuery(uri, proj, null, null, null);
            int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            actualimagecursor.moveToFirst();
            String img_path = actualimagecursor.getString(actual_image_column_index);
            File file = new File(img_path);
            LitePal.getDatabase();
            if (isExternalStorageReadable()) {
                try {
                    List<Investigation> list = DataSupport.findAll(Investigation.class);
                    if (list.size() == 0) {
                        if (ExcelUtils.readSheet1(file)) {
                            Toast.makeText(HomeActivity.this, "导入数据成功,文件已在" + file.getName().substring(0, 6) + "目录下", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(HomeActivity.this, MainActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(HomeActivity.this, "导入数据失败", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(HomeActivity.this, "导入数据成功,文件已在", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(HomeActivity.this, MainActivity.class);
                        startActivity(intent);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(HomeActivity.this, "导入数据失败", Toast.LENGTH_SHORT).show();
            }
            sharedPreferences = getSharedPreferences("myData", MODE_PRIVATE);
            editor = sharedPreferences.edit();
            editor.putString("excelName", file.getName().substring(0, 9));
            editor.commit();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.load_data:
                Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
                intent1.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
                intent1.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent1, 1);
                break;

        }
    }

    /* Checks if external storage is available to at least read */
    public boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }
}
